package com.eipc.util;

import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.Groups;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;

public class ContactList {
	 
 	private ArrayList<Contact> contacts = new ArrayList<Contact>();
 
 	public ArrayList<Contact> getContacts() {
 		return contacts;
 	}
 
 	public void setContacts(ArrayList<Contact> contacts) {
 		this.contacts = contacts;
 	}
 	
 	public void addContact(Contact contact) {
 		this.contacts.add(contact);
 	}
 	
 	public void insert(ContentResolver resolver){
 		for(Contact c : contacts){
 			c.insert(resolver);
 		}
 	}
  	
 	public ContactList() {}
 	
 	static class Contact {
 		public String id;
 		public String displayName;
 		public ArrayList<Phone> phone;
 		public ArrayList<Email> email;
 		public ArrayList<String> notes;
 		public ArrayList<IM> imAddresses;
 		public Organization organization;
 		
 		
 		public void insert(ContentResolver resolver){
 	 		ArrayList<ContentProviderOperation> operationList =
 	            new ArrayList<ContentProviderOperation>();  
 	        ContentProviderOperation.Builder builder =
 	            ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
// 	        String groupId = "";
// 	        /* 获得EIPC组的id */
// 	        final Cursor cursor = resolver.query(Groups.CONTENT_URI, new String[] {
// 	                Groups._ID},
// 	                Groups.TITLE + " = ? ", new String[] {
// 	                "EIPC" }, null);
// 	        try {
// 	            if (cursor != null && cursor.moveToFirst()) {
// 	            	groupId = cursor.getString(0);
// 	            }
// 	        }
// 	        catch(Exception e){
// 	        	Log.w("", e);
// 	        }
// 	        finally {
// 	            if (cursor != null) {
// 	                cursor.close();
// 	            }
// 	        }
 	      String account = null;
 	      String accountName = null;
 	      builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account);
 	      builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName);
 	      operationList.add(builder.build());
 	        
 	        
 	        /* 人的信息 */
 	        {	
 	            builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
 	            builder.withValueBackReference(StructuredName.RAW_CONTACT_ID, 0);
 	            builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
 	            builder.withValue(StructuredName.DISPLAY_NAME,displayName);
 	            operationList.add(builder.build());
 	        }
 	        
 	        /* 电话信息 */
 	       if (phone != null) {
 	            for (Phone p : phone) {
 	                builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
 	                builder.withValueBackReference(android.provider.ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, 0);
 	                builder.withValue(Data.MIMETYPE, android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
 	                builder.withValue(android.provider.ContactsContract.CommonDataKinds.Phone.LABEL, "EIPC");
 	                builder.withValue(android.provider.ContactsContract.CommonDataKinds.Phone.TYPE, p.type);
 	                builder.withValue(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER, p.number);
 	                builder.withValue(Data.IS_PRIMARY, 0);
 	                operationList.add(builder.build());
 	            }
 	        }
 	       
 	       /* 组织信息 */
 	      if (organization != null) {
 	                builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
 	                builder.withValueBackReference(android.provider.ContactsContract.CommonDataKinds.Organization.RAW_CONTACT_ID, 0);
 	                builder.withValue(Data.MIMETYPE, android.provider.ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
 	                builder.withValue(android.provider.ContactsContract.CommonDataKinds.Organization.TYPE, "");
 	                builder.withValue(android.provider.ContactsContract.CommonDataKinds.Organization.COMPANY, organization.organization);
 	                builder.withValue(android.provider.ContactsContract.CommonDataKinds.Organization.TITLE, organization.title);
 	                builder.withValue(Data.IS_PRIMARY, 1);
 	                operationList.add(builder.build());
 	        }
 	      
	 	      /* email */
	 	     if (email != null) {
	             for (Email e : email) {
	                 builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
	                 builder.withValueBackReference(android.provider.ContactsContract.CommonDataKinds.Email.RAW_CONTACT_ID, 0);
	                 builder.withValue(Data.MIMETYPE, android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
	
	                 builder.withValue(android.provider.ContactsContract.CommonDataKinds.Email.TYPE, e.type);
	                 builder.withValue(android.provider.ContactsContract.CommonDataKinds.Email.DATA, e.address);
	                 builder.withValue(Data.IS_PRIMARY, 1);
	                 operationList.add(builder.build());
	             }
	         }
	 	     
	 	     /* im */
	 	    if (imAddresses != null) {
	            for (IM imData : imAddresses) {
	                builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
	                builder.withValueBackReference(android.provider.ContactsContract.CommonDataKinds.Im.RAW_CONTACT_ID, 0);
	                builder.withValue(Data.MIMETYPE, android.provider.ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
	                
	                builder.withValue(android.provider.ContactsContract.CommonDataKinds.Im.TYPE, imData.type);
	                builder.withValue(android.provider.ContactsContract.CommonDataKinds.Im.DATA, imData.name);
	                builder.withValue(Data.IS_PRIMARY, 1);
	                operationList.add(builder.build());
	            }
	        }
	 	    
	 	   if (notes != null) {
	            for (String note : notes) {
	                builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
	                builder.withValueBackReference(Note.RAW_CONTACT_ID, 0);
	                builder.withValue(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE);

	                builder.withValue(Note.NOTE, note);
	                operationList.add(builder.build());
	            }
	        }
	 	   
//	 	   if (groupId != null) {
//	            builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
//	            builder.withValueBackReference(GroupMembership.RAW_CONTACT_ID, 0);
//	            builder.withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE);
//	            builder.withValue(GroupMembership.GROUP_ROW_ID, groupId);
//	            operationList.add(builder.build());
//	        }
	 	   try {
	            resolver.applyBatch(ContactsContract.AUTHORITY, operationList);
	        } catch (RemoteException e) {
	            Log.e("", String.format("%s: %s", e.toString(), e.getMessage()));
	        } catch (OperationApplicationException e) {
	            Log.e("", String.format("%s: %s", e.toString(), e.getMessage()));
	        }
 	 	}
 		
		public Contact(String id, String displayName, ArrayList<Phone> phone,
				ArrayList<Email> email, ArrayList<String> notes,
				ArrayList<IM> imAddresses,
				Organization organization) {
			this.id = id;
			this.displayName = displayName;
			this.phone = phone;
			this.email = email;
			this.notes = notes;
			this.imAddresses = imAddresses;
			this.organization = organization;
		}

		public Contact(String displayName, ArrayList<Phone> phone,
				ArrayList<Email> email, ArrayList<String> notes,
				ArrayList<IM> imAddresses,Organization organization) {
			this.displayName = displayName;
			this.phone = phone;
			this.email = email;
			this.notes = notes;
			this.imAddresses = imAddresses;
			this.organization = organization;
		}
 		
 		static class Email {
 			public String address;
 			public String type;
			public Email(String address, String type) {
				this.address = address;
				this.type = type;
			}
 		}
 		
 		static class IM {
 			public String name;
 			public String type;
			public IM(String name, String type) {
				this.name = name;
				this.type = type;
			}
 		}
 		
 		static class Organization {
 			public String organization = "";
 			public String title = "";
 		 	public Organization() {	}
 		 	public Organization(String org, String title) {
 		 		this.organization = org;
 		 		this.title = title;
 		 	}
 		}
 		
 		static class Phone {
 			public String number;
 			public int type;
 			
			public Phone(String number, int type) {
				this.number = number;
				this.type = type;
			}
 		 }
 	}
 }

